package com.rem.stream.part1;

import com.rem.lambda.part1.Employee;
import org.junit.Test;

import java.util.Arrays;
import java.util.Comparator;
import java.util.List;
import java.util.Optional;

/**
 * Stream 的终止操作====查找与匹配
 *
 * @Author Rem
 * @Date 2020-02-13
 */

public class TestStream5 {

    List<Employee> employees = Arrays.asList(
            new Employee("张三", 16, 3468.12, Employee.State.BUSY),
            new Employee("李四", 42, 9485.32, Employee.State.FREE),
            new Employee("王五", 24, 5124.4, Employee.State.VOCATION),
            new Employee("赵六", 33, 6381.82, Employee.State.FREE),
            new Employee("田七", 36, 4448.45, Employee.State.BUSY),
            new Employee("方八", 36, 7485.42, Employee.State.VOCATION)
    );


    /**
     * 查找与匹配
     * allMatch----检查是否匹配所有元素
     * anyMatch----检查是否至少匹配一个元素
     * noneMatch---检查是否没有匹配所有元素
     * findFirst---返回当前第一个元素
     * findAny-----返回当前流中的任意元素
     * count-------返回流中的总个数
     * max---------返回流中最大值
     * min---------返回流中最小值
     */
    //allMatch
    //查找员工是否都繁忙
    @Test
    public void test1() {
        boolean b1 = employees.stream()
                .allMatch(e -> e.getState().equals(Employee.State.BUSY));
        System.out.println(b1);
    }

    //anyMatch
    //查找员工是否有空闲的
    @Test
    public void test2() {
        boolean b2 = employees.stream()
                .anyMatch(e -> e.getState().equals(Employee.State.FREE));
        System.out.println(b2);
    }

    //noneMatch
    //查找员工是否都不繁忙
    @Test
    public void test3() {
        boolean b2 = employees.stream()
                .noneMatch(e -> e.getState().equals(Employee.State.BUSY));
        System.out.println(b2);
    }

    //findFirst
    //查找35岁以上按照列表排在第一个的位置的员工
    @Test
    public void test4() {
        Optional<Employee> optional = employees.stream()
                .filter(e -> e.getAge() > 35)
                .findFirst();
        System.out.println(optional.get());
    }

    //findAny
    //查找工资5000以上的任意一个员工
    @Test
    public void test5() {
        //并行流
        Optional<Employee> optional = employees.parallelStream()
                .filter(e -> e.getSalary() > 5000)
                .findAny();
        System.out.println(optional.get());
    }

    //count
    //查找工资5000以上员工有多少人
    @Test
    public void test6() {
        long count = employees.stream()
                .filter(e -> e.getSalary() > 5000)
                .count();
        System.out.println(count);
    }

    //max
    //查找年龄最大的人
    @Test
    public void test7() {
        Optional<Employee> optional = employees.stream()
                //max((e1, e2) -> Integer.compare(e1.getAge(), e2.getAge()))
                .max(Comparator.comparingInt(Employee::getAge));
        System.out.println(optional.get());
    }

    //max
    //查找最小工资是多少
    @Test
    public void test8() {
        Optional<Double> optional = employees.stream()
                .map(Employee::getSalary)
                .min(Double::compare);
        System.out.println(optional.get());
    }


}
